package com.huayue.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.support.spring.FastJsonJsonView;
import com.huayue.annotation.LogAnno;
import com.huayue.entity.Department;
import com.huayue.service.DepartmentService;
import com.huayue.util.PageHelper;

/**
 * 部门
 */
@Controller
@RequestMapping("/department")
public class DepartmentController {
    // 自动注入
    @Autowired
    DepartmentService svc;

    // 创建模板controller对象
    BaseController bc = new BaseController();

    /**
     * 加载页面的初始化
     * 
     * @return
     */
    @RequestMapping("/toAdd")
    public ModelAndView toAdd() {
        FastJsonJsonView view = new FastJsonJsonView();

        view.addStaticAttribute("status", 200);
        view.addStaticAttribute("data", svc.findAllTopDepartment());
        return new ModelAndView(view);
    }

    @LogAnno(operateType = "添加-部门")
    @RequestMapping("/add")
    public ModelAndView add(Department department) {
        return bc.add(svc, department);
    }

    @LogAnno(operateType = "修改-部门")
    @RequestMapping("/update")
    public ModelAndView update(Department department) {
        return bc.update(svc, department);
    }

    @LogAnno(operateType = "删除-部门")
    @RequestMapping("/delete")
    public ModelAndView delete(Department department) {
        return bc.delete(svc, department);
    }

    @RequestMapping(value = "/findOne", produces = {"text/javascript;charset=UTF-8"})
    public ModelAndView findOne(Department department) {
        // 先获取部门信息
        ModelAndView modelAndView = bc.findOne(svc, department);
        // 再添加部分数据
        modelAndView.addObject("topdepartment", svc.findAllTopDepartment());
        // 返回modelAndView的数据
        return modelAndView;
    }

    @RequestMapping("/findPage")
    public ModelAndView findPage(PageHelper page, Department department) {
        return bc.findPage(svc, page, department, department.getParentid());
    }

}
